package com.visitor.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.visitor.bean.VisitorRecord;
import com.visitor.mapper.VisitorRecordMapper;
import com.visitor.web.vm.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class VisitorRecordService {
	@Resource
	private VisitorRecordMapper VisitorRecordMapper;

	@Transactional
	public Response<String> save(VisitorRecord user) {
		VisitorRecordMapper.insert(user);
		return Response.ok();
	}
	
	public VisitorRecord findUserById(Long id) {
		VisitorRecord user = VisitorRecordMapper.selectByPrimaryKey(id);
		return user;
	}
	
	public int deleteUserById(Long id) {
		return VisitorRecordMapper.deleteByPrimaryKey(id);
	}
	
	public PageInfo<VisitorRecord> getAllByPage(int pageNum,int pageSize,String appointmentTime){
		List<VisitorRecord> list;
		PageHelper.startPage(pageNum, pageSize, true);
		if(null!=appointmentTime&&appointmentTime.length()>0){
			String[] time=appointmentTime.replaceAll(" ","").split("~");
			String startTime=time[0];
			String endTime=time[1];
			list= VisitorRecordMapper.selectAll(startTime,endTime);
		}else{
			list= VisitorRecordMapper.selectAll(null,null);
		}

		if(list==null||list.isEmpty()) {
			list = new ArrayList<>();
		}
		return new PageInfo<>(list);
	}

	public List<VisitorRecord> getAll(){
		return VisitorRecordMapper.selectAll(null,null);
	}
}
